<% allow_return_item_changes = !@return_authorization.customer_returned_items? %>

<div data-hook="admin_return_authorization_form_fields">
  <table class="index return-items-table">
    <thead>
      <tr data-hook="rma_header">
        <th class="return-item-checkbox">
          <% if allow_return_item_changes %>
            <%= check_box_tag 'select-all' %>
          <% end %>
        </th>
        <th class="return-item-product"><%= Spree::Product.model_name.human %></th>
        <th class="return-item-state"><%= Spree::ReturnAuthorization.human_attribute_name(:state) %></th>
        <th class="return-item-charged"><%= Spree::ReturnItem.human_attribute_name(:charged) %></th>
        <th class="return-item-pre-tax-refund-amount"><%= Spree::ReturnItem.human_attribute_name(:amount) %></th>
        <th class="return-item-reimbursement-type"><%= Spree::ReimbursementType.model_name.human %></th>
        <th class="return-item-exchange-for"><%= Spree::ReturnItem.human_attribute_name(:exchange_variant) %></th>
        <th class="return-item-reason"><%= Spree::ReturnItem.human_attribute_name(:return_reason) %></th>
      </tr>
    </thead>
    <tbody>
      <%= f.fields_for :return_items, @form_return_items do |item_fields| %>
        <% return_item = item_fields.object %>
        <% inventory_unit = return_item.inventory_unit %>
        <% editable = inventory_unit.shipped? && !inventory_unit.exchange_requested? && allow_return_item_changes && return_item.reimbursement.nil? %>
        <tr>
          <td class="return-item-checkbox align-center inventory-unit-checkbox">
            <% if editable %>
              <%= item_fields.hidden_field :inventory_unit_id %>
              <%= item_fields.check_box :_destroy, {checked: return_item.persisted?, class: 'add-item', "data-price" => return_item.amount}, '0', '1' %>
            <% end %>
          </td>
          <td class="return-item-product">
            <div class="variant-name"><%= inventory_unit.variant.name %></div>
            <div class="variant-options"><%= inventory_unit.variant.options_text %></div>
          </td>
          <td class="return-item-state align-center"><%= inventory_unit.state.humanize %></td>
          <td class="return-item-charged align-center">
            <%= return_item.display_amount %>
          </td>
          <td class="return-item-pre-tax-refund-amount align-center">
            <% if editable %>
              <%= item_fields.text_field :amount, { class: 'refund-amount-input' } %>
            <% else %>
              <%= return_item.display_amount %>
            <% end %>
          </td>
          <td class="return-item-reimbursement-type">
            <% if editable %>
              <%= item_fields.select :preferred_reimbursement_type_id, @reimbursement_types.collect{|r|[r.name.humanize, r.id]}, {include_blank: true}, {class: 'select2 fullwidth'} %>
            <% else %>
              <%= return_item.preferred_reimbursement_type.try(:name) %>
            <% end %>
          </td>
          <td class="return-item-exchange-for align-center">
            <% if editable %>
              <%= item_fields.collection_select :exchange_variant_id, return_item.eligible_exchange_variants(@stock_locations), :id, :exchange_name, { include_blank: true }, { class: "select2 fullwidth return-item-exchange-selection" } %>
            <% elsif return_item.exchange_processed? %>
              <%= return_item.exchange_variant.exchange_name %>
            <% end %>
          </td>
          <td class="return-item-reason">
            <% if editable %>
              <%= item_fields.select :return_reason_id, @reasons.collect{|r|[r.name, r.id]}, {include_blank: true}, {class: 'select2 fullwidth'} %>
            <% else %>
              <%= return_item.return_reason.try(:name) %>
            <% end %>
          </td>
        </tr>
      <% end %>
    </tbody>
  </table>

  <%= f.field_container :amount do %>
    <%= Spree.t(:total_pre_tax_refund) %>: <span id="total_pre_tax_refund">0.00</span>
  <% end %>

  <%= f.field_container :stock_location do %>
    <%= f.label :stock_location_id, Spree::StockLocation.model_name.human %>
    <%= f.select :stock_location_id, @stock_locations.to_a.collect{|l|[l.name, l.id]}, {include_blank: true}, {class: 'select2 fullwidth', "data-placeholder" => Spree.t(:select_a_stock_location)} %>
    <%= f.error_message_on :stock_location_id %>
  <% end %>

  <%= f.field_container :memo do %>
    <%= f.label :memo %>
    <%= f.text_area :memo, {:style => 'height:100px;', :class => 'fullwidth'} %>
    <%= f.error_message_on :memo %>
  <% end %>
</div>

<% if Spree::Config[:expedited_exchanges] %>
  <div class="expedited-exchanges-warning"><%= Spree.t(:expedited_exchanges_warning, days_window: Spree::Config[:expedited_exchanges_days_window]) %></div>
<% end %>
